package com.shadowtorcher.stdisk.serverlet;

import com.shadowtorcher.soulknife.SoulKnifeDB;
import com.shadowtorcher.soulknife.exception.BeanConfigErrorException;
import com.shadowtorcher.stdisk.bean.LogicFileBean;
import com.shadowtorcher.stdisk.bean.PhysicsFileBean;
import com.shadowtorcher.stdisk.bean.SharedFileBean;
import com.shadowtorcher.stdisk.bean.UserBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "FileShareServlet", urlPatterns = "/s/*")
public class FileShareServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //截取url后的code码存入sharedfile表URL
        String url = request.getRequestURI();
        String[] sourceStrArray = url.split("/");
        String code = sourceStrArray[sourceStrArray.length - 1];
        LogicFileBean logicfilebean = new LogicFileBean();
        SharedFileBean sharedfilebean = new SharedFileBean();
        PhysicsFileBean physicsfilebean = new PhysicsFileBean();
        UserBean userbean = new UserBean();
        //从数据库中取出链接对应的逻辑ID
        try {
            List<SharedFileBean> list = SoulKnifeDB.selectWhere("Url =?", code).setClass(SharedFileBean.class);
            sharedfilebean = list.get(0);


        } catch (BeanConfigErrorException | SQLException e) {
            e.printStackTrace();
        }

        //通过文件的逻辑ID获取数据库逻辑文件表中的相关数据并取得物理文件ID

        try {
            List<LogicFileBean> list = SoulKnifeDB.selectWhere("LogicFileID = ?", sharedfilebean.getLogicFileID()).setClass(LogicFileBean.class);
            logicfilebean = list.get(0);


        } catch (BeanConfigErrorException | SQLException e) {
            e.printStackTrace();
        }
        String physicsfileid = logicfilebean.getPhysicsFileID();
        String userid = logicfilebean.getUserID();
        //通过userid来查用户名
        try {
            List<UserBean> list = SoulKnifeDB.selectWhere("UserID = ?", userid).setClass(UserBean.class);
            userbean = list.get(0);
        } catch (BeanConfigErrorException | SQLException e) {
            e.printStackTrace();
        }
        //通过文件的物理文件ID获得文件大小信息
        try {
            List<PhysicsFileBean> list = SoulKnifeDB.selectWhere("PhysicsFileID = ?", physicsfileid).setClass(PhysicsFileBean.class);
            physicsfilebean = list.get(0);
        } catch (BeanConfigErrorException | SQLException e) {
            e.printStackTrace();
        }
        //数据回调
        request.setAttribute("logicfilebean", logicfilebean);
        request.setAttribute("physicsfilebean", physicsfilebean);
        request.setAttribute("userbean", userbean);
        request.getRequestDispatcher("../jsp/fileshare.jsp").forward(request, response);
    }
}
